package com.wanpinghui.wphlibrary.model.base;

import android.content.Context;
import android.media.MediaPlayer;

import com.wanpinghui.wphlibrary.model.db.manager.MsgDbManager;
import com.wanpinghui.wphlibrary.model.message.bean.MessagesExt;
import com.yunshl.yunshllibrary.manager.UpdateManager;
import com.yunshl.yunshllibrary.utils.LogUtils;
import com.yunshl.yunshllibrary.utils.TextUtil;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by xiongning on 17-10-30.
 */

public class AudioPlayManager {
    public static final int status_pause = 1;
    public static final int status_playing = 2;
    public static final int status_stop = 3;
    private static AudioPlayManager instance;

    private MediaPlayer mMediaPlayer;
    private OnPlayListener onPlayListener;
    private int status;
    private String souceUrl;

    public static AudioPlayManager getInstance(){

        if(instance==null){

            instance=new AudioPlayManager();
        }

        return instance;

    }


    public void play(String path, final OnPlayListener listener){
        souceUrl = path;
        onPlayListener = listener;
        if(mMediaPlayer!=null) {
            mMediaPlayer.reset();
        }else {
            mMediaPlayer = new MediaPlayer();
        }
        try {
//            if (path.startsWith("http")) {
//                new AppUpdateManager().download(path, new UpdateManager.IDownloadStatus() {
//                    @Override
//                    public void downloadFinish(final String fileName) {
//                        Observable.timer(0, TimeUnit.MILLISECONDS, Schedulers.io())
//                                .observeOn(AndroidSchedulers.mainThread())
//                                .subscribe(new Action1<Long>() {
//                                    @Override
//                                    public void call(Long aLong) {
//                                        try {
//                                            mMediaPlayer.setDataSource(fileName);
//                                        } catch (IOException e) {
//                                            e.printStackTrace();
//                                        }
//                                        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
//                                            @Override
//                                            public void onPrepared(MediaPlayer mp) {
//                                                mp.start();
//                                                if (listener != null) {
//                                                    mp.getDuration();
//                                                    listener.onStart(mp);
//                                                }
//                                            }
//                                        });
//                                        mMediaPlayer.prepareAsync();
//                                    }
//                                }, new Action1<Throwable>() {
//                                    @Override
//                                    public void call(Throwable throwable) {
//                                        throwable.printStackTrace();
//                                    }
//                                });
//                    }
//
//                    @Override
//                    public void downloadPause() {
//                    }
//
//                    @Override
//                    public void downloadStart(long fileLength) {
//                    }
//
//                    @Override
//                    public void downLoading(int progress, int second) {
//                    }
//
//                    @Override
//                    public void downLoadError(String message) {
//                        LogUtils.w("AudioPlayManager", "下载失败:"+message);
//                        if (listener != null) {
//                            listener.onError(mMediaPlayer);
//                        }
//                    }
//                });
//            }else {
                mMediaPlayer.setDataSource(path);
                mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mp.start();
                        status = status_playing;
                        if (listener != null) {
                            mp.getDuration();
                            listener.onStart(mp);
                        }
                    }
                });
                mMediaPlayer.prepareAsync();
//            }
        } catch (IOException e) {
            e.printStackTrace();
            if (listener != null) {
                listener.onError(mMediaPlayer);
            }
        }
        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                status = 0;
                if (listener != null) {
                    listener.onComplete(mp);
                }
            }
        });
        mMediaPlayer.start();//开始播放
    }

    public void stop(){
        status = status_stop;
        if(mMediaPlayer!=null) {
            mMediaPlayer.stop();
            mMediaPlayer.reset();
        }
        if (onPlayListener != null) {
            onPlayListener.onComplete(mMediaPlayer);
        }
    }

    public void start(String path, final OnPlayListener listener){
        if (TextUtil.isEmpty(path))return;
        if (mMediaPlayer != null && status == status_pause && path.equals(souceUrl)) {
            resume();
        }else {
            play(path, listener);
        }
    }

    public void resume(){
        status = status_playing;
        if(mMediaPlayer!=null) {
            mMediaPlayer.start();
            if (onPlayListener != null) {
                onPlayListener.onResume(mMediaPlayer);
            }
        }

    }

    public boolean isPlaying(){
        if(mMediaPlayer!=null)
            return mMediaPlayer.isPlaying();

        return false;

    }
    public void pause(){
        status = status_pause;
        if(mMediaPlayer!=null) {
            mMediaPlayer.pause();
            if (onPlayListener != null) {
                onPlayListener.onPause(mMediaPlayer);
            }
        }
    }

    public interface OnPlayListener{
        void onStart(MediaPlayer mediaPlayer);
        void onComplete(MediaPlayer mediaPlayer);
        void onError(MediaPlayer mediaPlayer);
        void onPause(MediaPlayer mediaPlayer);
        void onResume(MediaPlayer mediaPlayer);
    }
}
